﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using JN.Utility;
using Power.Common;
using Power.BLL;

namespace Power.WinUI
{
    public partial class LockUser : DevExpress.XtraEditors.XtraForm
    {
        #region property
        private string _userId;
        private string _stationId;

        public string UserId
        {
            get { return _userId; }
            set { _userId = value; }
        }
        public string StationId
        {
            get { return _stationId; }
            set { _stationId = value; }
        }
        #endregion
        public LockUser()
        {
            InitializeComponent();
        }

        private void LockUser_Load(object sender, EventArgs e)
        {
            //load info
            txtUserId.Text = _userId;
            txtUserId.Properties.ReadOnly = true;            
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            if (txtUserId.Text == "")
            {
                XtraMessageBox.Show("用户名错误!!", "提示");
                return;
            }
            if (VaildUser())
            {
                this.DialogResult = System.Windows.Forms.DialogResult.OK;
                this.Close();
            }
            else
            {
                XtraMessageBox.Show("用户密码错误，请重新输入!!", "提示");
                txtPassword.Focus();
            }
        }
        private bool VaildUser()
        {
            try
            {
                bool ret = false;
                string pwd = Power.Common.DEncrypt.DEncrypt.Encrypt(txtPassword.Text);
                StringBuilder sql = new StringBuilder();
                sql.Append(" user_id = '" + _userId + "'");
                sql.Append(" and user_password = '" + pwd + "' ");
                if (_stationId != "")
                {
                    sql.Append(" and station_id ='" + _stationId + "' ");
                }
                Sys_userBLL userBLL = new Sys_userBLL();
                DataTable dtUser = userBLL.GetList(sql.ToString()).Tables[0];
                if (dtUser.Rows.Count > 0)
                {
                    ret = true;
                }

                return ret;
            }
            catch (Exception ex)
            {
                SystemLogger.Write(ex);
                return false;
            }
        }

        private void txtPassword_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                btnOk_Click(sender, e);
            }
        }
    }
}